/**
 * 多边形手绘工具  左键添加点  右键删除点  双击结束
 * @param {Earth} options.earth 渲染容器
 * @param {Array} options.color 边框颜色 colorArray
 * @param {Array} options.fillColor 填充颜色 colorArray
 * 
 * @returns {DrawPolygonTool} DrawPolygonTool实例
 */

function DrawPolygonTool (options) {
    this.earth = options.earth
    this.color = options.color
    this.fillColor = options.fillColor
    
    this.init()
}

// 初始化
DrawPolygonTool.prototype.init = function () {
    this.handler = null
    this.line1 = null
    this.line2 = null
    this.polygon = null
    this.line1Points = []
    this.line2Points = []
    this.pointList = []

    this.polygonInit()
    this.lineInit()
    // this.start()
}

// 初始化线段
DrawPolygonTool.prototype.lineInit = function () {
    let objConfig = {
        positions: []
    }
    let line1 = new XE.Obj.Polyline(this.earth);
    line1.xbsjFromJSON(objConfig);
    line1.material.type = "XbsjPolylineDashMaterial";
    line1.material.XbsjPolylineDashMaterial.gapColor = [1,1,1,0];
    line1.material.XbsjPolylineDashMaterial.color = this.color || [0,0,1,1];
    this.line1 = line1

    let line2 = new XE.Obj.Polyline(this.earth);
    line2.xbsjFromJSON(objConfig);
    line2.material.type = "XbsjPolylineDashMaterial";
    line2.material.XbsjPolylineDashMaterial.gapColor = [1,1,1,0];
    line2.material.XbsjPolylineDashMaterial.color = this.color || [0,0,1,1];
    this.line2 = line2
}

// 初始化多边形
DrawPolygonTool.prototype.polygonInit = function () {
    let objConfig = {
        color: this.fillColor || [ 1, 1, 1, 0.2 ],
        outline: {
            color: this.color || [ 0, 0, 1, 1 ],
            width: 3,
            show: true
        },
        // ground: true
    }
    let polygon = new XE.Obj.Polygon(this.earth);
    polygon.xbsjFromJSON(objConfig);

    polygon._node = this
    this.polygon = polygon
}

// 添加点击监听
DrawPolygonTool.prototype.click = function () {
    if (this.onclick) { this.onclick() }
}


// 开始绘制
DrawPolygonTool.prototype.start = function () {
    let that = this
    let canvasbox = this.earth.czm.viewer.canvas
    this.handler = new Cesium.ScreenSpaceEventHandler(canvasbox);
    let mouseClick = false
    let doubleClick = true
    let mouseRightClick = false

    // 单机增加点位
    this.handler.setInputAction(function (e) {
        mouseClick = true
        setTimeout(() => {
            mouseClick = false
        }, 200)
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN)

    this.handler.setInputAction(function (e) {
        if (!mouseClick || !doubleClick) {
            return
        }

        let position = that.earth.pickPosition({ x: e.position.x, y: e.position.y });
        that.pointList = that.pointList.concat([ position[0], position[1] ])

        if (that.pointList.length > 2) {
            that.polygon.positions = that.pointList
            // 设置2线段第一个点
            that.line2Points[0] = [ position[0], position[1], 0 ]
        } else {
            // 设置1线段第一个点
            that.line1Points[0] = [ position[0], position[1], 0 ]
        }

        // 双击防抖
        doubleClick = false
        setTimeout(() => {
            doubleClick = true
        }, 200)
    }, Cesium.ScreenSpaceEventType.LEFT_UP)

    // 双击结束
    this.handler.setInputAction(function (e) {
        that.off()
    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)

    // 右键移除一个点
    this.handler.setInputAction(function (e) {
        mouseRightClick = true
        setTimeout(() => {
            mouseRightClick = false
        }, 200)
    }, Cesium.ScreenSpaceEventType.RIGHT_DOWN)

    this.handler.setInputAction(function (e) {
        if (!mouseRightClick) {
            return
        }
        that.pointList.splice(-2, 2)
        that.polygon.positions = that.pointList
        that.line2Points[0] = [ that.pointList.slice(-2)[0], that.pointList.slice(-1)[0], 0 ]
        that.line2.positions = that.line2Points

    }, Cesium.ScreenSpaceEventType.RIGHT_UP)


    // 鼠标移动带动虚线
    this.handler.setInputAction(function (e) {
        
        let position = that.earth.pickPosition({ x: e.endPosition.x, y: e.endPosition.y });
        if (that.line1Points.length > 0) {
            that.line1Points[1] = [ position[0], position[1], 0 ]
            that.line1.positions = that.line1Points
        }
        if (that.line2Points.length > 0) {
            that.line2Points[1] = [ position[0], position[1], 0 ]
            that.line2.positions = that.line2Points
        }

    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)

}

// 停止绘制 解除监听
DrawPolygonTool.prototype.off = function () {
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN)
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_DOWN)
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_UP)
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
    if (this.line1) { this.line1.destroy() }
    if (this.line2) { this.line2.destroy() }
}

// 获取多边形
DrawPolygonTool.prototype.getPolygon = function () {
    let radian = 180 / Math.PI;
    let list = this.pointList.map(item => {
        return item * radian
    })
    return list
}

// 销毁监听器与实例
DrawPolygonTool.prototype.destroy = function () {
    this.off()
    this.handler.destroy()
    this.polygon.destroy()
}


export default DrawPolygonTool